package com.inossem.warehousestorage_inossem.request;

import android.content.Context;

import com.inossem.utils.exception.InossemException;
import com.inossem.utils.request.VolleyUtils;
import com.inossem.warehousestorage_inossem.bean.common.Bean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;

/**
 * Created by leij on 2017/6/13.
 */

public class Request {

    protected static void excuteJsonBean(Context context, Bean bean, String code, String msg, GsonBeanListener gsonBeanListener) {
        if (RequestConstant.CODE_SUCCESS.equals(code)) {
            gsonBeanListener.onSuccess(bean);
        } else {
            if (gsonBeanListener.useDefaultError()) {
                gsonBeanListener.onDefaultError(context, code, msg);
            } else {
                gsonBeanListener.onError(context, code, msg);
            }
        }
    }

    protected static void requestException(String url) {
        throw new InossemException("request send fail!, request is null, url:" + url);
    }

    protected static String addRequest(Context context, com.android.volley.Request<?> request, String url) {
        String tag = null;
        if (request != null) {
            tag = (String) request.getTag();
            VolleyUtils.addRequest(context, request);
        } else {
            requestException(url);
        }
        return tag;
    }

}
